<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="84px"
        :rules="formRules"
      >
        <el-form-item label="岗位名称" prop="postName">
          <el-input
            v-model="formData.postName"
            placeholder="请输入岗位名称"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input
            v-model="formData.postCode"
            placeholder="请输入岗位编码"
            clearable
          />
        </el-form-item>
        <el-form-item label="排序" prop="postSort">
          <div>
            <el-input-number v-model="formData.postSort" :min="0" :max="9999" />
            <div class="form-tips">默认为0， 数值越大越排前</div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            placeholder="请输入备注"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-switch
            v-model="formData.status"
            :active-value="'0'"
            :inactive-value="'1'"
          />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { postEdit, postAdd, postDetail } from "@/api/org/post";
import Popup from "@/components/popup/index.vue";
import feedback from "@/utils/feedback";
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑岗位" : "新增岗位";
});
const formData = reactive({
  postCode: "",
  postName: "",
  postId: "",
  postSort: 0,
  remark: "",
  status: undefined,
});

const formRules = {
  postCode: [
    {
      required: true,
      message: "请输入岗位编码",
      trigger: ["blur"],
    },
  ],
  postName: [
    {
      required: true,
      message: "请输入岗位名称",
      trigger: ["blur"],
    },
  ],
};

const handleSubmit = async () => {
  await formRef.value?.validate();
  mode.value == "edit" ? await postEdit(formData) : await postAdd(formData);
  feedback.msgSuccess("操作成功");
  popupRef.value?.close();
  emit("success");
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await postDetail(row.postId);
  setFormData(data);
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
